package sword_offer;

/**
 * @author Synhard
 * @version 1.0
 * @Class sword_offer.Code4
 * @Description 请实现一个函数，把字符串 s 中的每个空格替换成"%20"。
 * 输入：s = "We are happy."
 * 输出："We%20are%20happy."
 * @tel 13001321080
 * @email 823436512@qq.com
 * @date 2021-03-12 16:02
 */
public class Code4 {
    public static void main(String[] args) {
        System.out.println(format("We are happy."));
    }
    private static String format(String s) {
        char[] target = s.toCharArray();
        TempStack<Character> stack = new TempStack<>(target.length * 3);
        for (char c : target) {
            if (c == ' ') {
                stack.push('%');
                stack.push('2');
                stack.push('0');
            } else {
                stack.push(c);
            }
        }

        char[] arr = new char[stack.getLength()];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (char) stack.getValue().toArray()[i];
        }
        return new String(arr, 0, stack.getLength());
    }
}


